package com.jml.zlhq.app.adapter;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

public class TeamLinGanAdapter extends IBaseAdapter {
	private List<JSONObject> itemList;
	private AppContext appContext;
	@SuppressWarnings("unused")
	private Activity activity;

	public TeamLinGanAdapter(Activity activity,  List<JSONObject> itemList) {
		this.itemList = itemList;
		this.activity = activity;
		this.appContext = (AppContext) activity.getApplication();
	}

	@Override
	public int getCount() {
		return itemList == null ? 0 : itemList.size();
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	static class ViewHolder {
		public ImageView img;
		public TextView name,addr,tag;
		public TextView todo,price,collect,comment;
	}

	@Override
	public View getView(final int position, View convertView, final ViewGroup parent) {
		
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			int layoutId = R.layout.team_lingan_item;
			convertView = LayoutInflater.from(appContext).inflate(layoutId, null);
			viewHolder.img = (ImageView) convertView.findViewById(R.id.team_lingan_item_img);
			int height = UIUtil.winWidth*320/640;
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height);
			viewHolder.img.setLayoutParams(lp);
			viewHolder.name = (TextView) convertView.findViewById(R.id.team_lingan_item_name);
			viewHolder.addr = (TextView) convertView.findViewById(R.id.team_lingan_locat);
			viewHolder.tag = (TextView) convertView.findViewById(R.id.team_lingan_item_tag);
			viewHolder.todo = (TextView) convertView.findViewById(R.id.team_lingan_item_todo);
			viewHolder.price = (TextView) convertView.findViewById(R.id.team_lingan_item_price);
			viewHolder.collect = (TextView) convertView.findViewById(R.id.team_lingan_item_collect);
			viewHolder.comment = (TextView) convertView.findViewById(R.id.team_lingan_item_comment);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final JSONObject json = itemList.get(position);
		try {
			ImageLoader.getInstance().displayImage(BaseTools.getFrom("cover.image_url", json), viewHolder.img, UIUtil.getImageLoaderDisplayOptions(R.drawable.default140));
			viewHolder.name.setText(BaseTools.getFrom("title", json));
			viewHolder.addr.setText(BaseTools.getFrom("place", json));
			JSONArray tags = json.get("data").toString().equals("null")?new JSONArray():json.getJSONArray("tags");
			String tagStr = "";
			for (int i = 0;tags!=null && i < tags.length(); i++) {
				tagStr += StringUtil.isEmpty(tagStr)?tags.getString(i):"&nbsp;&nbsp;"+tags.getString(i);
			}
			viewHolder.tag.setText(Html.fromHtml(tagStr));
			viewHolder.tag.setVisibility(tags==null||tags.length()==0?View.GONE:View.VISIBLE);
			String privce = BaseTools.getFrom("wedding_arrangement", json);
			viewHolder.todo.setText("婚庆布置");
			viewHolder.price.setText(StringUtil.isEmpty(privce)||"0".equals(privce)?"":"￥"+BaseTools.getFrom("wedding_arrangement", json));
			viewHolder.collect.setText(BaseTools.getFrom("statistic.COLLECT", json));
			viewHolder.comment.setText(BaseTools.getFrom("statistic.COMMENT", json));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return convertView;
	}

	
	
	

}
